<template>
  <a-drawer
    :title="formTitle"
    width="35%"
    :label-col="4"
    :wrapper-col="14"
    :visible="open"
    @close="onClose">
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="姓名" prop="name" >
        <a-input v-model="form.name" placeholder="请输入姓名" />
      </a-form-model-item>
      <a-form-model-item label="性别" prop="sex">
        <a-radio-group v-model="form.sex" button-style="solid">
          <a-radio-button v-for="(d, index) in sexOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="出生日期" prop="birthDate" >
        <a-date-picker style="width: 100%" v-model="form.birthDate" format="YYYY-MM-DD" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="户口地" prop="home" >
        <a-input v-model="form.home" placeholder="请输入户口地" />
      </a-form-model-item>
      <a-form-model-item label="居住地" prop="live" >
        <a-input v-model="form.live" placeholder="请输入居住地" />
      </a-form-model-item>
      <a-form-model-item label="工作状态" prop="jobState" >
        <a-select
          v-model="form.jobState"
          placeholder="请选择工作状态"
        >
          <a-select-option v-for="(d, index) in jobStateOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="毕业院校" prop="school" >
        <a-select
          v-model="form.school"
          placeholder="请选择院校"
        >
          <a-select-option v-for="(d, index) in schoolOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="最高学历" prop="degree" >
        <a-select
          v-model="form.degree"
          placeholder="请选择学历"
        >
          <a-select-option v-for="(d, index) in eduLevelOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="就读专业" prop="professional" >
        <a-select
          v-model="form.professional"
          placeholder="请选择专业"
        >
          <a-select-option v-for="(d, index) in schoolProfessionalOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="就读时间" prop="startDate" >
        <a-month-picker style="width: 45%" v-model="form.startDate" format="YYYY-MM" allow-clear/>
        &nbsp;至&nbsp;
        <a-month-picker style="width: 45%" v-model="form.endDate" format="YYYY-MM" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="意向岗位" prop="post" >
        <a-select
          label-in-value
          v-model="form.post"
          placeholder="请选择意向岗位"
          @change="handlePostChange"
        >
          <a-select-option
            v-for="(d, index) in postOptions"
            :key="index"
            :value="d.postId">
            {{ d.postName }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="自我评价" prop="evaluation" >
        <a-input v-model="form.evaluation" placeholder="请输入评价" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getPersonnel, addPersonnel, updatePersonnel } from '@/api/platform/personnel'

export default {
  name: 'CreateForm',
  props: {
    sexOptions: {
      type: Array,
      required: true
    },
    jobStateOptions: {
      type: Array,
      required: true
    },
    eduLevelOptions: {
      type: Array,
      required: true
    },
    schoolProfessionalOptions: {
      type: Array,
      required: true
    },
    schoolOptions: {
      type: Array,
      required: true
    },
    postOptions: {
      type: Array,
      required: true
    }
  },
  components: {

  },
  data () {
    return {
      loading: false,
      formTitle: '',
      // 表单参数
      form: {
        personnelId: undefined,
        userId: undefined,
        deptId: undefined,
        layoutImages: undefined,
        name: undefined,
        sex: undefined,
        birthDate: undefined,
        home: undefined,
        live: undefined,
        degree: undefined,
        jobState: undefined,
        school: undefined,
        startDate: undefined,
        endDate: undefined,
        professional: undefined,
        evaluation: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'change' }],
        sex: [{ required: true, message: '性别不能为空', trigger: 'change' }],
        birthDate: [{ required: true, message: '出生日期不能为空', trigger: 'change' }],
        home: [{ required: true, message: '户口地不能为空', trigger: 'change' }],
        live: [{ required: true, message: '居住地不能为空', trigger: 'change' }],
        jobState: [{ required: true, message: '工作状态不能为空', trigger: 'change' }],
        degree: [{ required: true, message: '学历不能为空', trigger: 'change' }],
        professional: [{ required: true, message: '专业不能为空', trigger: 'change' }],
        startDate: [{ required: true, message: '时间段不能为空', trigger: 'change' }],
        school: [{ required: true, message: '院校不能为空', trigger: 'change' }]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        personnelId: undefined,
        userId: undefined,
        deptId: undefined,
        layoutImages: undefined,
        name: undefined,
        sex: undefined,
        birthDate: undefined,
        home: undefined,
        live: undefined,
        degree: undefined,
        jobState: undefined,
        school: undefined,
        startDate: undefined,
        endDate: undefined,
        professional: undefined,
        evaluation: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加信息'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const personnelId = row ? row.personnelId : ids
      getPersonnel(personnelId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改信息'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.personnelId !== undefined && this.form.personnelId !== null) {
            updatePersonnel(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addPersonnel(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    },
    /** 岗位下拉框搜索 */
    handlePostChange (value) {
      this.form.postId = value.key
      this.form.postName = value.label.replaceAll(' ', '')
      console.log(this.form)
    }
  }
}
</script>
